package com.heaven.back.pojo;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Random;
import java.util.function.Predicate;
import java.util.Set;

/**
 * The type Room.
 */
@Data
public class Room {
    /**
     * -- GETTER --
     * Gets description.
     *
     * @return the description
     */
    @Getter
    private final String description;
    /**
     * -- SETTER --
     * Sets teleport.
     * @param teleport the teleport
     */
    @Setter
    private Boolean teleport = false;
    private final HashMap<String, Room> exits = new HashMap<>();
    private final ArrayList<Item> items = new ArrayList<>();

    /**
     * Instantiates a new Room.
     *
     * @param description the description
     */
    public Room(String description) {
        this.description = description;
    }

    /**
     * Sets exit.
     *
     * @param direction the direction
     * @param neighbor  the neighbor
     */
    public void setExit(String direction, Room neighbor) {
        exits.put(direction, neighbor);
    }

    /**
     * Gets long description.
     *
     * @param game   the game
     * @param player the player
     * @return the long description
     */
    public String getLongDescription(Game game, Player player) {
        StringBuilder returnString = new StringBuilder("你现在正在");
        returnString.append(description).append(".\n");
        if (!teleport) {
            returnString.append(getExitString());
        } else {
            returnString.append('\n').append("由于你不小心踏入了一个废弃传送门......");
            returnString.append("你突然陷入一阵晕眩...片刻后，醒来后发现......\n");

            Random random = new Random();
            int randomIndex = random.nextInt(game.getGameMap().size() - 1);
            //随机产生待选房间索引
            Predicate<Room> pr = a -> (a.description.equals(this.description));
            Collection<Room> rooms = game.getGameMap().values();
            rooms.removeIf(pr);//移除房间本身

            //game.setCurrentRoom( (Room)rooms.toArray()[randomIndex]);
            //随机进入房间
            //returnString.append(game.getCurrentRoom().getLongDescription(game));
            Room current = (Room) rooms.toArray()[randomIndex];
            player.setCurrentRoom(current.getDescription());
            returnString.append(current.getLongDescription(game, player));
        }
        return returnString.toString();
    }

    private String getExitString() {
        StringBuilder returnString = new StringBuilder("出口:");
        Set<String> keys = exits.keySet();
        for (String exit : keys) {
            returnString.append(" ").append(exit);
        }
        return returnString.toString();
    }

    /**
     * Get item item.
     *
     * @param item the item
     * @return the item
     */
    public Item getItem(String item) {
        for (Item i : items) {
            if (i.getName().equals(item)) {
                return i;
            }
        }
        return null;
    }

    /**
     * Add item.
     *
     * @param item the item
     */
    public void addItem(Item item) {
        items.add(item);
    }

    /**
     * Remove item.
     *
     * @param item the item
     */
    public void removeItem(int item) {
        items.remove(item);
    }

    /**
     * Gets exit.
     *
     * @param direction the direction
     * @return the exit
     */
    public Room getExit(String direction) {
        return exits.get(direction);
    }

}
